

using System;
using System.Data.SqlClient;
using System.Data;

namespace GeneralClasses
{

    ///holds permission and keeps track of it in the database

    #region NOTES
    /*
     * 3 NOV 2010 - cleaning it up
13 OCT 2010 - Created class, cant put it in so nothing is tested, still writing in notepadd++.
*/
    #endregion


    public class Permission
    {

        #region VARIABLES
        //==============================================================================
        //                                                                     VARIABLES
        //==============================================================================

        String name;
        int permissionID;

        DatabaseConnection dbc;

        #endregion

        #region PROPERTIES
        //==============================================================================
        //                                                                    PROPERTIES
        //==============================================================================

        /*public String ConnectionString
        {
            get { return dbc.ConnectionString; }
            set { dbc.ConnectionString = value; }
        }*/

        public int PermissionID
        {
            get { return permissionID; }
            set { permissionID = value; }
        }

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        #endregion

        #region CONSTRUCTOR
        //==============================================================================
        //                                                                   CONSTRUCTOR
        //==============================================================================

        public Permission()
        {
            permissionID = -1; // not in table by default
            name = "Allowed to Exist"; // default
            dbc = new DatabaseConnection();
        }

        #endregion

        #region PRIVATE

        //==============================================================================
        //                                                                       PRIVATE
        //==============================================================================

        private int removePermissionRelations()
        {
            try
            {
                if (permissionID != -1)
                {
                    dbc.executeNonQuery("DELETE FROM titlehaspermissionrelationship WHERE peid='" + this.permissionID + "'");
                    return 0;
                }
                return -1;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }

        private bool checkPermissionUsed()
        {
            try
            {
                if (this.permissionID != -1)
                {
                    DataTable table = dbc.query("SELECT * FROM titlehaspermissionrelationship WHERE peid='" + this.permissionID + "'");
                    if ((table != null) && table.Rows.Count > 0) return true;
                }
                return false;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }

        /// <summary>
        /// returns whether or not its in the database
        /// </summary>
        /// <returns></returns>
        private bool checkPermissionExists()
        {
            try
            {
                //well it loos like i couldnt put it all in one line without problems. 
                DataTable table = dbc.query("SELECT name FROM Permission WHERE peid = '" + permissionID + "'");
                return ((table != null) && table.Rows.Count > 0) ? (true) : (false);
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }

        private int findPermissionID()
        {
            try
            {
                DataTable table = dbc.query("SELECT peid FROM Permission WHERE name = '" + name + "'");
                if ((table != null) && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        return (int)row["peid"];
                    }
                }
                return -1;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }

        #endregion

        #region PUBLIC
        //==============================================================================
        //                                                                        PUBLIC
        //==============================================================================

        public void loadData()
        {
            try
            {
                DataTable table = dbc.query("SELECT * FROM Permission WHERE peid = '" + permissionID + "'");
                if ((table != null) && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        this.permissionID = (int)row["peid"]; // this isnt really necessary, but ill keep it for now
                        name = (string)row["name"];
                    }
                }
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }

        }

        public int savedata()
        {
            try
            {
                if (this.permissionID == -1)
                {
                    if (!checkPermissionExists())
                    {
                        dbc.executeNonQuery("INSERT INTO Permission (name) values ('" + name + "')");
                    }
                    return (permissionID = findPermissionID());
                }
                else
                {
                    //save
                    dbc.executeNonQuery("UPDATE Permission SET name='" + this.name + "' WHERE peid='" + permissionID + "'");
                    return permissionID;
                }
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }

        }

        public int removeData()
        {
            try
            {
                if (this.permissionID != -1)
                {
                    removePermissionRelations();
                    if (!checkPermissionUsed())
                    {
                        dbc.executeNonQuery("DELETE FROM Permission WHERE peid='" + this.permissionID + "'");
                        return 0;
                    }
                }
                return -1;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }

        public void deprecated_deletePermission() { }
        public void deprecated_addPermission() { }
        public int deprecated_getPermissionID() { return -1; }

        #endregion

    } // end class


} //end namespace